package com.haierCamera.customapplication.ui.user;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.haierCamera.customapplication.BaseFragment;
import com.haierCamera.customapplication.Constants;
import com.haierCamera.customapplication.R;
import com.haierCamera.customapplication.databinding.HzklFragmentMineBinding;
import com.haierCamera.customapplication.ui.NavigationController;
import com.haierCamera.customapplication.ui.user.fragment.HZKLUserLoginFragment;
import com.haierCamera.customapplication.ui.user.fragment.HZKLUserNotFragment;
import com.haierCamera.customapplication.utils.SharedPreferencesUtils;
import com.haierCamera.customapplication.utils.android.HttpErrorProcess;

import javax.inject.Inject;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.databinding.DataBindingUtil;
import androidx.fragment.app.FragmentTransaction;

import java.io.OutputStream;
import java.net.Socket;

import dagger.Lazy;

public class MineFragment extends BaseFragment {

    //    @Inject
//    ViewModelProvider.Factory factory;
    @Inject
    Lazy<HttpErrorProcess> httpErrorProcessLazy;

    @Inject
    Lazy<NavigationController> navigationController;
    HzklFragmentMineBinding binding;
    private FragmentTransaction fragmentTransaction;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        binding = DataBindingUtil.inflate(inflater, R.layout.hzkl_fragment_mine, container, false);
        initData();
        return binding.getRoot();
    }

    private void initData() {
        fragmentTransaction = getActivity().getSupportFragmentManager().beginTransaction();
        String token = SharedPreferencesUtils.getString(Constants.SP_USER_TOKEN, "");
        //fragmentTransaction.replace(R.id.minFrame, new HZKLUserNotFragment()).commit();
        if (TextUtils.isEmpty(token)) {
            fragmentTransaction.replace(R.id.minFrame, new HZKLUserNotFragment()).commit();
        } else {
            fragmentTransaction.replace(R.id.minFrame, new HZKLUserLoginFragment()).commit();
        }
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        //因为最外面的fragment使用了hide隐藏，所以进这个界面第一次会加载这个fragment，第二次就是show显示，不会调用生命周期方法
        if (!hidden) {
            fragmentTransaction = getActivity().getSupportFragmentManager().beginTransaction();
            String token = SharedPreferencesUtils.getString(Constants.SP_USER_TOKEN, "");
            //fragmentTransaction.replace(R.id.minFrame, new HZKLUserNotFragment()).commit();
            if (TextUtils.isEmpty(token)) {
                fragmentTransaction.replace(R.id.minFrame, new HZKLUserNotFragment()).commit();
            } else {
                fragmentTransaction.replace(R.id.minFrame, new HZKLUserLoginFragment()).commit();
            }
        }
    }

    @Override
    public void onResume() {
        super.onResume();
        fragmentTransaction = getActivity().getSupportFragmentManager().beginTransaction();
        String token = SharedPreferencesUtils.getString(Constants.SP_USER_TOKEN, "");
        //fragmentTransaction.replace(R.id.minFrame, new HZKLUserNotFragment()).commit();
        if (TextUtils.isEmpty(token)) {
            fragmentTransaction.replace(R.id.minFrame, new HZKLUserNotFragment()).commit();
        } else {
            fragmentTransaction.replace(R.id.minFrame, new HZKLUserLoginFragment()).commit();
        }
    }
}
